/* Magic Mirror
 * Node Helper: MMM-ReceiveData
 *
 * By coffee_java
 * MIT Licensed.
 */

var NodeHelper = require("node_helper");
var bodyParser = require('body-parser');

module.exports = NodeHelper.create({
	// Subclass start method.
	start: function() {
		var self = this;
		console.log("Starting module helper: " + self.name);
		self.expressApp.use(bodyParser.json());
		self.expressApp.use(bodyParser.urlencoded({ extended: false }));
		
		self.expressApp.post("/postdata", function(req, res) {
			console.log("receive post");
			var jsonData="";
			if(req.body.jsonData){
				jsonData=req.body.jsonData;
			}
			console.log("receive post data:"+jsonData);
			if(jsonData==""){
				res.send("没有解析到数据");
			}else{
				try { //异常捕获
					var jsonArray = JSON.parse(jsonData);
					self.sendSocketNotification("UPDATE_DATA", {
						jsonData: jsonArray
					});
				} catch (err) {
					console.log("read json error:"+err);
				}
				res.send('OK');
			}
			
		});
	},
	// Subclass socketNotificationReceived received.
	socketNotificationReceived: function(notification, payload) {
		console.log("socketNotificationReceived:"+notification);
	}
});